import json
import requests
from lxml import etree

def save_filename(filename,content):
    with open(filename, "w+", encoding="utf-8") as f:
        f.write(json.dumps(content, ensure_ascii=False, indent=2))
def do_request(url):
    # 请求网页
    response = requests.get(url)
    # 设置编码方式
    response.encoding = "utf-8"
    return response.text
def parse_html(html):
    # 解析网页
    node = etree.HTML(html)
    # titles
    titles = node.xpath("//div[@class='list-item']/h4/a/text()")
    # 日期
    dates = node.xpath("//div[@class='list-item']/div/text()")
    items = []
    for j in range(len(titles)):
        d = {}
        d["标题"] = str(titles[j]).strip()
        d["时间"] = str(dates[j]).strip()
        items.append(d)
    print(items)
    return items
def create_url(start_page,end_page):
    base_url = "https://www.hbjcxy.com/jyyw/index" #.html
    for i in range(start_page,end_page):
        if i==0:
            url = base_url + ".html"
            html = do_request(url)
            content = parse_html(html)
            save_filename("标题和日期/"+str(i)+".json",content)
        else:
            url = base_url + "_" + str(i + 1) + ".html"
            html = do_request(url)
            content = parse_html(html)
            save_filename("标题和日期/" + str(i) + ".json", content)
if __name__ == '__main__':
    create_url(0,5)